﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bus.Bll;
using Bus.Model;
using System.Configuration;
public partial class admin_brush : System.Web.UI.Page
{
    protected tb_custom cusBll = new tb_custom();
    protected tb_cost costbll = new tb_cost();
    protected tb_costcard ccbll = new tb_costcard();
    protected CostcardModel ccm = null;
    protected CostModel cm = null;
    protected LogModel lm = null;
    protected CustomModel cusM = null;
    protected tb_log logbll = new tb_log();
    protected int cid;
    protected List<ConfigModel> list = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        cid = int.Parse(Request.QueryString["id"].ToString());
        if (!IsPostBack)
        {
            InitcusInfo();            
           
        }
    }
    private void InitcusInfo()
    {
        try
        {
            cusM = new CustomModel();
            cusM = cusBll.GetModel(cid);
            cm = new CostModel();
            cm = costbll.GetModel(cid);
            cusname.Text = cusM.cname;
            if (cusM.ctype == 0)
                custp.Text = "老年人";
            if (cusM.ctype == 1)
                custp.Text = "成年人";
            if (cusM.ctype == 2)
                custp.Text = "学生";
            cuskd.Text = cusM.Nmcrad;
            cusxy.Text = cm.money.ToString();
            if (decimal.Parse(cm.money.ToString()) == decimal.Parse("0"))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('当前可用余额为【00.00】,请充值！');</script>");
                Button1.Enabled = false;
            }

        }
        catch (Exception e)
        {
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (double.Parse(cusxy.Text) > 0)
                Button1.Enabled = true;
            //消费记录
            double v = double.Parse(ConfigurationManager.AppSettings["value"].ToString());
            double indbv = 0;
            list = new List<ConfigModel>();
            list = new tb_config().GetModelList("");
            cusM = new CustomModel();
            cusM = cusBll.GetModel(cid);
            if (cusM.ctype == 0)
                indbv = double.Parse(list.Find((c) => c.configname == tb_config.lrstr).val.ToString()) / 100 * v;
            if (cusM.ctype == 1)
            {
                
                indbv = double.Parse(list.Find((c) => c.configname == tb_config.crstr).val.ToString()) / 100 * v;
            }
            if(cusM .ctype ==2)
                indbv = double.Parse(list.Find((c) => c.configname == tb_config.xsstr).val.ToString()) / 100 * v;
            ccm = new CostcardModel();
            ccm.cid = cid;
            ccm.costdate = DateTime.Now;
            ccm.vl = decimal.Parse(indbv.ToString());//本次消费额
            bool f = ccbll.Add(ccm);
            if (f )
            {
                jz.Text = indbv.ToString()+"元";
                cusxy.Text = (double.Parse(cusxy.Text) - indbv).ToString();//余额
                Bus .Dal .DbHelperSQL.ExecuteSql ("update tb_cost set [money]=[money]-"+indbv+" where id="+cid);//扣费操作
                lm = new LogModel();
                lm.userid = cid;
                lm.op = "消费";
                lm.memo = "消费" +indbv.ToString ();
                lm.opdate = DateTime.Now;
                logbll.Add(lm);//记录日志
              
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('刷卡【" + indbv + "】元成功！');</script>");
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('刷卡【" + indbv + "】元失败！');</script>");
            }
        }
        catch (Exception ee) { }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        //清除当前登录的用户及类型
        Session["userid"] = null;
        Session["utp"] = null;
        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>top.location.href='../Default.aspx';</script>");
    }
}
